<template>
  <div>
   <Header></Header>
    <div class="block">
      <div class="radio">
        排序：
        <el-radio-group v-model="reverse">
          <el-radio :label="true">倒序</el-radio>
          <el-radio :label="false">正序</el-radio>
        </el-radio-group>
      </div>

      <el-timeline :reverse="reverse">
        <el-timeline-item
            v-for="(collection, index) in collections"
            :key="index">
          <el-row>
            <el-col :span="4">
              <el-card :body-style="{ padding: '0px' }">
                <img :src="collection.pic" class="image">
                <div style="padding: 14px;">
                  <span>{{ collection.name }} </span>
                  <div class="bottom clearfix">
                    <time class="time">{{ collection.createdTime }}</time>
                    <el-button type="text" class="button" @click="unCollection(collection.id)">取消收藏</el-button>
                  </div>
                </div>
              </el-card>
            </el-col>
          </el-row>
        </el-timeline-item>
      </el-timeline>
    </div>
  </div>
</template>

<script>
import Header from "../components/Header";
export default {
  name: "MyCollection.vue",
  components: {Header},
  data() {
    return {
      user:this.$store.getters.getUser,
      reverse: true,
      collections: {},
    };
  },
  methods:{
    unCollection(id){
      const _this = this
      _this.$axios.post("/collection/delete/"+id).then(res => {
        if(res.data.data===true){
          this.$message({
            showClose: true,
            message: res.data.message,
            type: 'success'
          });
          const userId=this.user.id
          this.$axios.post('/collection/getMyCollection/' + userId).then(res => {
            const collections = res.data.data
            this.collections=collections
          })
        }else{
          this.$message.error("取消收藏失败");
        }
      })
    }
  },
  created() {
    const userId=this.user.id
    //异步请求查询
    if(userId){
      this.$axios.post('/collection/getMyCollection/' + userId).then(res => {
        const collections = res.data.data
        this.collections=collections

      })
    }
  }
};
</script>
<style>
.time {
  font-size: 13px;
  color: #999;
}

.bottom {
  margin-top: 13px;
  line-height: 12px;
}

.button {
  padding: 0;
  float: right;
}

.image {
  width: 100%;
  display: block;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}
</style>